package com.guava.collection;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.Tables;

import java.util.Map;
import java.util.Set;

/**
 * TableTester--GuavaTest
 *
 * @author: Jovi
 * @createTime: 2018-05-08 15:54
 **/
public class TableTester {

    public static void main(String[] args) {
        //Table<R,C,V> == Map<R,Map<C,V>>
        /*
         *  Company: IBM, Microsoft, TCS
         *  IBM 		-> {101:Mahesh, 102:Ramesh, 103:Suresh}
         *  Microsoft 	-> {101:Sohan, 102:Mohan, 103:Rohan }
         *  TCS 		-> {101:Ram, 102: Shyam, 103: Sunil }
         *
         * */
        Table<String, String, String> empTable = getEmpTable();

        System.out.println(empTable.size());

        Set<Table.Cell<String, String, String>> cells = empTable.cellSet();

        for (Table.Cell<String, String, String> cell : cells) {
            System.out.println(cell.getRowKey() + "--" + cell.getColumnKey() + "--" + cell.getValue());
        }

        System.out.println("-------rowKey和columnKey转换---------");
        Table<String, String, String> transposeTable = Tables.transpose(empTable);
        Set<Table.Cell<String, String, String>> cells1 = transposeTable.cellSet();
        for (Table.Cell<String, String, String> cell : cells1) {
            System.out.println(cell.getRowKey() + "--" + cell.getColumnKey() + "--" + cell.getValue());
        }

        System.out.println("=========================================");

        Map<String, String> ibm = empTable.row("IBM");
        System.out.println("List of IBM Employees");
        for (Map.Entry<String, String> entry : ibm.entrySet()) {
            System.out.println("Emp Id : " + entry.getKey() + ", Name:" + entry.getValue());
        }

        Set<String> employers = empTable.rowKeySet();
        System.out.print("Employers:");
        for (String employer : employers) {
            System.out.print(employer + " ");
        }

        System.out.println();
        Map<String, String> column = empTable.column("102");
        for (Map.Entry<String, String> entry : column.entrySet()) {
            System.out.println("Employer:" + entry.getKey() + ", Name:" + entry.getValue());
        }
    }

    private static Table<String, String, String> getEmpTable() {
        //create a table
        Table<String, String, String> empTable = HashBasedTable.create();

        empTable.put("IBM", "101", "Mahesh");
        empTable.put("IBM", "102", "Ramesh");
        empTable.put("IBM", "103", "Suresh");

        empTable.put("Microsoft", "101", "Sohan");
        empTable.put("Microsoft", "102", "Mohan");
        empTable.put("Microsoft", "103", "Rohan");

        empTable.put("TCS", "101", "Ram");
        empTable.put("TCS", "102", "Shyam");
        empTable.put("TCS", "103", "Sunil");
        return empTable;
    }

}
